<template>
  <div>
    <el-col :xl="6" :lg="8">
      <el-form-item label="项目地址" prop="address">
        <el-input v-model="formData.address" clearable placeholder="" size="small" @click.native="selectRegion" />
      </el-form-item>
    </el-col>
    <el-col :xl="6" :lg="8">
      <el-form-item label="坐标经度" prop="x1">
        <el-input v-model="formData.x1" clearable size="small" />
      </el-form-item>
    </el-col>
    <el-col :xl="6" :lg="8">
      <el-form-item label="坐标纬度" prop="y1">
        <el-input v-model="formData.y1" clearable size="small" />
      </el-form-item>
    </el-col>
    <MapDialog :dialog-visible="dialogVisible" :location="province" @getPointInfo="getPointInfo" @cancelDialog="dialogVisible = $event" />
  </div>
</template>

<script>
// 百度组件
import MapDialog from './components/MapDialog'
export default {
  components: {
    MapDialog
  },
  props: {
    // 获取城市 例：石家庄市裕华区
    province: {
      type: String,
      default: ''
    },
    address: {
      type: String,
      default: ''
    },
    x1: {
      type: Number,
      default: 0
    },
    y1: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      // 弹框显隐
      dialogVisible: false,
      // 表单
      formData: {
        address: '', // 详细地址
        x1: '', // 经度
        y1: '' // 纬度
      }
    }
  },
  watch: {
    province(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.formData = { address: '', x1: '', y1: '' }
      }
    },
    x1(newVal) {
      if (newVal) {
        this.formData.x1 = newVal
        this.formData.address = this.address
        this.formData.y1 = this.y1
        console.log(this.formData)
      }
    }
  },
  methods: {
    // 选择详细地址
    selectRegion() {
      if (this.province.length === 0) {
        this.msgWarning('请先选择区域')
        return
      }
      this.dialogVisible = true
    },
    // 获取经纬度和地址
    getPointInfo(data) {
      this.dialogVisible = false
      this.formData = { ...data }
      this.$emit('getPosition', this.formData)
    }
  }
}
</script>

<style>

</style>
